<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="厂站信息编号" prop="plantStationsId">
        <el-input v-model="queryParams.plantStationsId" placeholder="请输入厂站信息编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="时间维度" prop="timeDimension">
        <el-input v-model="queryParams.timeDimension" placeholder="请输入时间维度" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="统计年份" prop="statisticYear">
        <el-input v-model="queryParams.statisticYear" placeholder="请输入统计年份" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="统计月份" prop="statisticMonth">
        <el-input v-model="queryParams.statisticMonth" placeholder="请输入统计月份" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="统计日期" prop="statisticDay">
        <el-input v-model="queryParams.statisticDay" placeholder="请输入统计日期" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="电量" prop="electricityConsumption">
        <el-input v-model="queryParams.electricityConsumption" placeholder="请输入电量" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="电费" prop="electricityCost">
        <el-input v-model="queryParams.electricityCost" placeholder="请输入电费" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="泵组 ID" prop="pumpGroupId">
        <el-input v-model="queryParams.pumpGroupId" placeholder="请输入泵组 ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="每电耗数据" prop="unitElectricityConsumption">
        <el-input v-model="queryParams.unitElectricityConsumption" placeholder="请输入每电耗数据" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="出口压力" prop="outletPressure">
        <el-input v-model="queryParams.outletPressure" placeholder="请输入出口压力" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="出口流量" prop="outletFlow">
        <el-input v-model="queryParams.outletFlow" placeholder="请输入出口流量" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="创建人" prop="creator">
        <el-input v-model="queryParams.creator" placeholder="请输入创建人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="更新人" prop="updater">
        <el-input v-model="queryParams.updater" placeholder="请输入更新人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="预期节能效果" prop="expectedEnergySavingEffect">
        <el-input v-model="queryParams.expectedEnergySavingEffect" placeholder="请输入预期节能效果" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['system:dispatchEnergyConsumption:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:dispatchEnergyConsumption:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:dispatchEnergyConsumption:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['system:dispatchEnergyConsumption:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dispatchEnergyConsumptionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="能耗分析编号" align="center" prop="energyConsumptionId" />
      <el-table-column label="厂站信息编号" align="center" prop="plantStationsId" />
      <el-table-column label="时间维度" align="center" prop="timeDimension" />
      <el-table-column label="统计年份" align="center" prop="statisticYear" />
      <el-table-column label="统计月份" align="center" prop="statisticMonth" />
      <el-table-column label="统计日期" align="center" prop="statisticDay" />
      <el-table-column label="电量" align="center" prop="electricityConsumption" />
      <el-table-column label="电费" align="center" prop="electricityCost" />
      <el-table-column label="泵组 ID" align="center" prop="pumpGroupId" />
      <el-table-column label="每电耗数据" align="center" prop="unitElectricityConsumption" />
      <el-table-column label="出口压力" align="center" prop="outletPressure" />
      <el-table-column label="出口流量" align="center" prop="outletFlow" />
      <el-table-column label="优化开停泵方案建议" align="center" prop="optimizationSuggestion" />
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="更新人" align="center" prop="updater" />
      <el-table-column label="预期节能效果" align="center" prop="expectedEnergySavingEffect" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dispatchEnergyConsumption:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:dispatchEnergyConsumption:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改能耗分析对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="dispatchEnergyConsumptionRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="厂站信息编号" prop="plantStationsId">
          <el-input v-model="form.plantStationsId" placeholder="请输入厂站信息编号" />
        </el-form-item>
        <el-form-item label="时间维度" prop="timeDimension">
          <el-input v-model="form.timeDimension" placeholder="请输入时间维度" />
        </el-form-item>
        <el-form-item label="统计年份" prop="statisticYear">
          <el-input v-model="form.statisticYear" placeholder="请输入统计年份" />
        </el-form-item>
        <el-form-item label="统计月份" prop="statisticMonth">
          <el-input v-model="form.statisticMonth" placeholder="请输入统计月份" />
        </el-form-item>
        <el-form-item label="统计日期" prop="statisticDay">
          <el-input v-model="form.statisticDay" placeholder="请输入统计日期" />
        </el-form-item>
        <el-form-item label="电量" prop="electricityConsumption">
          <el-input v-model="form.electricityConsumption" placeholder="请输入电量" />
        </el-form-item>
        <el-form-item label="电费" prop="electricityCost">
          <el-input v-model="form.electricityCost" placeholder="请输入电费" />
        </el-form-item>
        <el-form-item label="泵组 ID" prop="pumpGroupId">
          <el-input v-model="form.pumpGroupId" placeholder="请输入泵组 ID" />
        </el-form-item>
        <el-form-item label="每电耗数据" prop="unitElectricityConsumption">
          <el-input v-model="form.unitElectricityConsumption" placeholder="请输入每电耗数据" />
        </el-form-item>
        <el-form-item label="出口压力" prop="outletPressure">
          <el-input v-model="form.outletPressure" placeholder="请输入出口压力" />
        </el-form-item>
        <el-form-item label="出口流量" prop="outletFlow">
          <el-input v-model="form.outletFlow" placeholder="请输入出口流量" />
        </el-form-item>
        <el-form-item label="优化开停泵方案建议" prop="optimizationSuggestion">
          <el-input v-model="form.optimizationSuggestion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="更新人" prop="updater">
          <el-input v-model="form.updater" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="预期节能效果" prop="expectedEnergySavingEffect">
          <el-input v-model="form.expectedEnergySavingEffect" placeholder="请输入预期节能效果" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DispatchEnergyConsumption">
import { listDispatchEnergyConsumption, getDispatchEnergyConsumption, delDispatchEnergyConsumption, addDispatchEnergyConsumption, updateDispatchEnergyConsumption } from "@/api/dispatch/analyze/dispatchEnergyConsumption";

const { proxy } = getCurrentInstance();

const dispatchEnergyConsumptionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    plantStationsId: null,
    timeDimension: null,
    statisticYear: null,
    statisticMonth: null,
    statisticDay: null,
    electricityConsumption: null,
    electricityCost: null,
    pumpGroupId: null,
    unitElectricityConsumption: null,
    outletPressure: null,
    outletFlow: null,
    optimizationSuggestion: null,
    creator: null,
    updater: null,
    expectedEnergySavingEffect: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询能耗分析列表 */
function getList() {
  loading.value = true;
  listDispatchEnergyConsumption(queryParams.value).then(response => {
    dispatchEnergyConsumptionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    energyConsumptionId: null,
    plantStationsId: null,
    timeDimension: null,
    statisticYear: null,
    statisticMonth: null,
    statisticDay: null,
    electricityConsumption: null,
    electricityCost: null,
    pumpGroupId: null,
    unitElectricityConsumption: null,
    outletPressure: null,
    outletFlow: null,
    optimizationSuggestion: null,
    createTime: null,
    creator: null,
    updater: null,
    updateTime: null,
    expectedEnergySavingEffect: null
  };
  proxy.resetForm("dispatchEnergyConsumptionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.energyConsumptionId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加能耗分析";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _energyConsumptionId = row.energyConsumptionId || ids.value
  getDispatchEnergyConsumption(_energyConsumptionId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改能耗分析";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["dispatchEnergyConsumptionRef"].validate(valid => {
    if (valid) {
      if (form.value.energyConsumptionId != null) {
        updateDispatchEnergyConsumption(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDispatchEnergyConsumption(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _energyConsumptionIds = row.energyConsumptionId || ids.value;
  proxy.$modal.confirm('是否确认删除能耗分析编号为"' + _energyConsumptionIds + '"的数据项？').then(function () {
    return delDispatchEnergyConsumption(_energyConsumptionIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/dispatchEnergyConsumption/export', {
    ...queryParams.value
  }, `dispatchEnergyConsumption_${new Date().getTime()}.xlsx`)
}

getList();
</script>
